package com.morningstar.system.feign;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.morningstar.system.api.entity.SysUserFeign;
import com.morningstar.system.api.feign.ISystemUserFeign;
import com.morningstar.system.dao.entity.SysDept;
import com.morningstar.system.dao.entity.SysUser;
import com.morningstar.system.dao.entity.SysUserPost;
import com.morningstar.system.dao.entity.SysUserRole;
import com.morningstar.system.service.*;
import jakarta.annotation.Resource;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author yanglianshan
 */
@RestController
public class SysUserFeignController implements ISystemUserFeign {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysMenuService menuService;

    @Resource
    private SysRoleService roleService;

    @Resource
    private SysDeptService sysDeptService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private SysUserPostService sysUserPostService;

    @Override
    public SysUserFeign getUserById(String userId) throws Exception {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUserId, userId);
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(sysUser)) {
            return null;
        }
        SysUserFeign sysUserFeign = new SysUserFeign();
        BeanUtil.copyProperties(sysUser, sysUserFeign);
        return sysUserFeign;
    }

    @Override
    public SysUserFeign getUserByName(String loginName) throws Exception {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getLoginName, loginName);
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(sysUser)) {
            return null;
        }
        SysUserFeign sysUserFeign = new SysUserFeign();
        BeanUtil.copyProperties(sysUser, sysUserFeign);
        return sysUserFeign;
    }

    @Override
    public SysUserFeign getUserByMobile(String mobile) throws Exception {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getPhone, mobile);
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(sysUser)) {
            return null;
        }
        SysUserFeign sysUserFeign = new SysUserFeign();
        BeanUtil.copyProperties(sysUser, sysUserFeign);
        return sysUserFeign;
    }

    @Override
    public List<String> getPermissionList(String loginId, String loginType) {
        return menuService.selectMenuPerms(Long.parseLong(loginId));
    }

    @Override
    public List<String> getRoleList(String loginId, String loginType) {
        return roleService.selectUserRoleKeys(Long.parseLong(loginId));
    }

    @Override
    public List<Long> selectUserByIds(List<Long> ids) {
        return this.getUserIds(ids);
    }

    @Override
    public List<Long> selectUserByRoleIds(List<Long> ids) {
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysUserRole::getRoleId, ids);
        List<Long> userIds = sysUserRoleService.list(queryWrapper).stream().map(SysUserRole::getUserId).toList();
        return this.getUserIds(userIds);
    }

    @Override
    public List<Long> selectUserByDeptIds(List<Long> ids) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysUser::getDeptId, ids);
        return sysUserService.list(queryWrapper).stream().map(SysUser::getUserId).toList();
    }

    @Override
    public List<Long> selectUserByPostIds(List<Long> ids) {
        LambdaQueryWrapper<SysUserPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysUserPost::getPostId, ids);
        List<Long> userIds = sysUserPostService.list(queryWrapper).stream().map(SysUserPost::getUserId).toList();
        return this.getUserIds(userIds);
    }

    @Override
    public List<Long> selectUserByDeptLeaderIds(List<Long> ids) {
        LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysDept::getDeptId, ids);
        List<Long> userIds = sysDeptService.list(queryWrapper).stream().map(SysDept::getLeader).toList();
        return this.getUserIds(userIds);
    }

    @Override
    public Map<Long, String> selectUserIdAndName() {
        return sysUserService.selectUserIdAndName();
    }

    @Override
    public void userLoginSuccess(Long loginId, String ip) {
        sysUserService.userLoginSuccess(loginId, ip);
    }


    private List<Long> getUserIds(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysUser::getUserId, ids);
        return sysUserService.list(queryWrapper).stream().map(SysUser::getUserId).toList();
    }
}
